package com.example.teamtribe.main.chat.recent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.common.UsersAdapter;
import com.example.teamtribe.database.ChattingBox;
import com.example.teamtribe.database.Request;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.main.chat.chatting.ChatActivity;
import com.example.teamtribe.main.chat.request.RequestActivity;
import com.example.teamtribe.utils.Utility;

import java.util.List;

public class RecentAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {

    private List data;
    private Context context;

    public RecentAdapter(List data) {
        this.data = data;
    }

    @Override
    public UsersAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_chat, parent, false);

        final UsersAdapter.ViewHolder holder = new UsersAdapter.ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(context, RequestActivity.class);
                if (viewType == ITEM_TYPE.REQUEST.ordinal()) {
                    Request request = (Request) data.get(position);
                    request.setRead(1);//只要不是0就是已读
                    request.saveOrUpdate();
                    data.set(position, request);
                    intent.putExtra("request", request);
                    ((Activity) context).startActivityForResult(intent, RecentFragment.REQUEST_REQU);
                    return;
                }
                if (viewType == ITEM_TYPE.TEAM_REQUEST.ordinal()) {
                    TeamRequest request = (TeamRequest) data.get(position);
                    request.setRead(true);
                    request.saveOrUpdate();
                    data.set(position, request);
                    intent.putExtra("request", request);
                    ((Activity) context).startActivityForResult(intent, RecentFragment.REQUEST_REQU);
                }
                if (viewType == ITEM_TYPE.CHATTING.ordinal()) {
                    ChattingBox chattingBox = (ChattingBox) data.get(position);
                    Intent chat = new Intent(context, ChatActivity.class);
                    chat.putExtra("user", Utility.getT(chattingBox.getUserId(), User.class));
                    context.startActivity(chat);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(UsersAdapter.ViewHolder holder, int position) {
        Object obj = data.get(position);
        if (obj instanceof Request) {
            Request request = (Request) obj;
            if (request.isRead() == 0) {
                holder.badge.setBadgeNumber(1);
            } else {
                holder.badge.hide(false);
            }
            User user = Utility.getT(request.getUserId(), User.class);
            if (user != null) {
                Glide.with(context).load(Utility.getUri(user.getPortrait())).into(holder.ivHead);
                holder.tvNickname.setText(user.getNickname());
                holder.tvInfo.setText(request.getRequestReason());
            }
            return;
        }
        if (obj instanceof TeamRequest) {
            TeamRequest request = (TeamRequest) obj;
            if (!request.isRead()) {
                holder.badge.setBadgeNumber(1);
            } else {
                holder.badge.hide(false);
            }
            Team team = Utility.getT(request.getTeamId(), Team.class);
            Glide.with(context).load(Utility.getUri(team.getPortrait())).into(holder.ivHead);
            holder.tvNickname.setText(team.getName());
            holder.tvInfo.setText(team.getComment());
        }
        if (obj instanceof ChattingBox) {
            ChattingBox chattingBox = (ChattingBox) obj;
            User user = Utility.getT(chattingBox.getUserId(), User.class);
            Glide.with(context).load(Utility.getUri(user.getPortrait())).into(holder.ivHead);
            holder.tvNickname.setText(user.getNickname());
            if (chattingBox.getRecent() != null)
                holder.tvInfo.setText(chattingBox.getRecent().getContend());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (data.get(position) instanceof Request) {
            return ITEM_TYPE.REQUEST.ordinal();
        }
        if (data.get(position) instanceof TeamRequest) {
            return ITEM_TYPE.TEAM_REQUEST.ordinal();
        }
        if (data.get(position) instanceof ChattingBox) {
            return ITEM_TYPE.CHATTING.ordinal();
        }
        return ITEM_TYPE.REQUEST.ordinal();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public enum ITEM_TYPE {
        REQUEST,
        TEAM_REQUEST,
        CHATTING
    }
}
